/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import blueprint4j.Constants;
import blueprint4j.utils.BindFieldInterface;
import blueprint4j.utils.BindFileLocationInterface;
import blueprint4j.utils.Bindable;
import blueprint4j.utils.FieldBinding;
import blueprint4j.utils.Log;

public class DataPairFileLocation extends DataPairDefault implements FieldBinding,ActionListener {
    private BindFileLocationInterface field;
    private static Icon icon = null;
    
    static {
        try {
            icon = Gui.loadIconsFromResource(
                    new String[]{Constants.ICON_BASE+"filesystems/folder.png"}
            )[0];
        } catch (Throwable th) {
            th.printStackTrace();
            Log.critical.out(th);
        }
    }
    
    private JTextField text_field = null;
    private JButton locate = new JButton(icon);
    
    private final JFileChooser fc = new JFileChooser();
    
    public DataPairFileLocation(Binder binder,BindFileLocationInterface p_field) {
        this(binder,p_field, true);
    }
    
    public DataPairFileLocation(Binder binder,BindFileLocationInterface p_field, boolean editable) {
        super(binder,p_field);
        text_field = new JTextField(20);
        field = p_field;
        setLabel(new JLabel(field.getDescription()));
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(text_field,BorderLayout.WEST);
        panel.add(locate,BorderLayout.EAST);
        locate.addActionListener(this);
        setComponent(panel);
        text_field.setEditable(editable);
        locate.setEnabled(editable);
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    }
    
    public boolean isChanged() {
        String f = field.get();
        if (f == null) {
            return f != text_field.getText();
        } else {
            return !f.equals(text_field.getText());
        }
    }
    
    public void intoControl() {
        text_field.setText(field.get());
    }
    
    public void intoBindable() {
        field.set(text_field.getText());
    }
    
    public Bindable getBindable() {
        return field.getBindable();
    }
    
    public BindFieldInterface getBindField() {
        return (BindFieldInterface) field;
    }
    
    public void setBindField(BindFieldInterface p_field) {
        field = (BindFileLocationInterface) p_field;
    }
    
    public void resetControl() {
        text_field.setText("");
    }
    
    public void actionPerformed(ActionEvent event) {
        int returnVal = fc.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            if (file.exists()) {
                if (file.isDirectory()) {
                    text_field.setText(file.getAbsolutePath());
                } else {
                    JOptionPane.showMessageDialog(null,"THAT IS NOT A DIRECTORY");
                }
            } else {
                JOptionPane.showMessageDialog(null,"THAT DIRECTORY DOES NOT EXIST");
            }
        }
    }
}

